package com.kun.service_edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.service_edu.entity.EduChapter;
import com.kun.service_edu.entity.EduVideo;
import com.kun.service_edu.entity.vo.chapter.ChapterVo;
import com.kun.service_edu.entity.vo.chapter.VideoVo;
import com.kun.service_edu.mapper.EduChapterMapper;
import com.kun.service_edu.service.EduChapterService;
import com.kun.service_edu.service.EduVideoService;
import com.kun.servicebase.exceptionhandler.KunException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;
    @Override
    public List<ChapterVo> getChapterServiceVideoByCourseId(String courseId) {

        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(chapterQueryWrapper);
        QueryWrapper<EduVideo> VideoQueryWrapper = new QueryWrapper<>();
        VideoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(VideoQueryWrapper);

        //创建list 用于封装
        ArrayList<ChapterVo> list = new ArrayList<>();
        //遍历查询章节list  进行封装
        for (int i=0;i<eduChapterList.size();i++){
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            list.add(chapterVo);
            //遍历查询小节list 进行封装
            ArrayList<VideoVo> videoList = new ArrayList<>();
            for (int j=0;j<eduVideoList.size();j++){
                EduVideo eduVideo = eduVideoList.get(j);
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoList.add(videoVo);
                }

            }
            chapterVo.setChildren(videoList);
        }
        return list;
    }
    @Override
    public boolean delChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        if (count>0){
            throw new KunException(20001,"不能删除");
        }else {
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }


    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
